package com.vincent.jvmtest.Jdbc;

import com.alibaba.druid.sql.PagerUtils;
import org.apache.calcite.sql.*;
import org.apache.calcite.sql.dialect.MysqlSqlDialect;
import org.apache.calcite.sql.parser.SqlParseException;
import org.apache.calcite.sql.parser.SqlParser;
import org.apache.calcite.sql.parser.SqlParserPos;

public class CalciteTest {
    public static void main(String[] args) throws SqlParseException {
        String sql = "select name,age,birthday from tableA where name like '%sa' and age=18 " +
                "and birthday >'1998-05-06 00:00:00' and birthday <= to_date('1998-05-06 00:00:00', 'yyyy-mm-dd hh24:mi:ss') and dept in (select * from tb2) and name in ('xixi',hh)";
        String limit = PagerUtils.limit(sql, "mysql", 0, 10);
        SqlParser sqlParser = SqlParser.create(sql);
        SqlNode sqlNode = sqlParser.parseQuery();
        SqlSelect sqlSelect = (SqlSelect) sqlNode;
        SqlNode where = sqlSelect.getWhere();
        SqlNodeList selectList = sqlSelect.getSelectList();
        SqlNode from = sqlSelect.getFrom();
        System.out.println(sqlSelect.toSqlString(MysqlSqlDialect.DEFAULT));
        SqlParser sqlParser2 = SqlParser.create("birthday < '1998-05-06 00:00:00'");
        SqlNode sqlNode1 = sqlParser2.parseExpression();
        SqlParserPos parserPosition = sqlNode1.getParserPosition();

        System.out.println(sqlNode1);
    }

    public void visit(SqlNode sqlNode) throws SqlParseException {
        if (sqlNode instanceof SqlIdentifier) {
            SqlIdentifier identifier = (SqlIdentifier) sqlNode;
            System.out.println("条件字段为："+identifier.getSimple());
        }
    }
}
